package com.example.team.service.Imp;

import com.example.team.dao.IVehicleUsageDao;
import com.example.team.entity.VehicleUsage;
import com.example.team.service.IVehicleUsageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 编写者：蓝江富
 * 时间：2023年10月31日15:51:04
 */
//服务接口规范的方法实现
@Service
public class IVehicleUsageServiceImpl implements IVehicleUsageService {

    @Autowired
    private IVehicleUsageDao vehicleUsageMapper;
    @Override
    public int insertVehicleUsage(VehicleUsage vehicleUsage) {
        //设置成功状态
        int vehicleUsageValue;
        VehicleUsage vehicleUsage1=new VehicleUsage();
        vehicleUsage1.setUsageId(vehicleUsage.getUsageId());
        VehicleUsage allVehicleUsage=vehicleUsageMapper.getVehicleUsageById(vehicleUsage1);
        if(allVehicleUsage!=null){
            vehicleUsageValue = -1;
        }else {
            vehicleUsageValue= vehicleUsageMapper.insertVehicleUsage(vehicleUsage);
        }
        //返回状态码
        return vehicleUsageValue;
    }

    @Override
    public int deleteVehicleUsage(VehicleUsage vehicleUsage) {
        //设置成功状态
        int vehicleUsageValue;
        VehicleUsage vehicleUsage1=new VehicleUsage();
        vehicleUsage1.setApplicant(vehicleUsage.getApplicant());
        List<VehicleUsage> allVehicleUsage=vehicleUsageMapper.getAllVehicleUsage(vehicleUsage1);
        if(allVehicleUsage.size()==0){
            vehicleUsageValue = -1;
        }else {
            vehicleUsageValue= vehicleUsageMapper.deleteVehicleUsage(vehicleUsage);
        }
        //返回状态码
        return vehicleUsageValue;
    }

    @Override
    public int updateVehicleUsage(VehicleUsage vehicleUsage) {
        //设置成功状态
        int vehicleUsageValue;
        //通过主键id对数据更新，判断是否存在目标id
        VehicleUsage vehicleUsage1=new VehicleUsage();
        vehicleUsage1.setUsageId(vehicleUsage.getUsageId());
        VehicleUsage allVehicleUsage=vehicleUsageMapper.getVehicleUsageById(vehicleUsage1);
        if(allVehicleUsage==null){
            //如果为空，说明无法找到匹配的目标Id，设置状态码-1
            vehicleUsageValue = -1;
        }else {
            //否则执行更新数据库功能，同时赋值更新成功的数据数量到状态码
            vehicleUsageValue= vehicleUsageMapper.updateVehicleUsage(vehicleUsage);
        }
        //返回状态码
        return vehicleUsageValue;
    }

    @Override
    public List<VehicleUsage> getAllVehicleUsage(VehicleUsage vehicleUsage) {
        return vehicleUsageMapper.getAllVehicleUsage(vehicleUsage);
    }

    @Override
    public VehicleUsage getVehicleUsage(VehicleUsage vehicleUsage) {
        return vehicleUsageMapper.getVehicleUsage(vehicleUsage);
    }
}
